//
//  SYLoginViewController.m
//  MicroNews
//
//  Created by siyue on 15/10/23.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYLoginViewController.h"
#import "SYRegisterViewController.h"
#import "SYLoginDataMaster.h"
#import "DialogUtil.h"
#import "SVProgressHUD.h"
#import "UIViewController+MMDrawerController.h"

@interface SYLoginViewController ()
- (IBAction)backClick:(id)sender;
- (IBAction)registerClick:(id)sender;
- (IBAction)forgetPWClick:(id)sender;
- (IBAction)loginClick:(id)sender;
- (IBAction)WZLLoginClik:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;


@end

@implementation SYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    UINavigationItem *leftItem = [[UINavigationItem alloc] initWithTitle:<#(nonnull NSString *)#>];
    
//    UIImage *image = [UIImage imageNamed:@"fanhui"];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
//    self.navigationItem.leftBarButtonItem = leftItem;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 10, 18)];
    [button setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)back {
    [self.mm_drawerController setMaximumLeftDrawerWidth:300];
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}


#pragma mark - net

- (void)requestLogin
{
    [SVProgressHUD show];
    
    NSString *username = [NSString stringWithFormat:@"%@",self.userNameTextField.text];
    NSString *password = [NSString stringWithFormat:@"%@",self.passwordTextField.text];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",HomeURL,User_Login_URL];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:username forKey:@"username"];
    [params setObject:password forKey:@"password"];
    [[NetworkInterface shareInstance] requestForPost:url parms:params complete:^(NSDictionary *result) {
        [SVProgressHUD dismiss];
        NSString *code = [NSString stringWithFormat:@"%@",result[@"code"]];
        if ([code isEqualToString:@"1"]) {
            NSString *message = [NSString stringWithFormat:@"%@",result[@"message"]];
            [[DialogUtil sharedInstance] showDlg:self.view textOnly:message];
            NSString *auth_code = [NSString stringWithFormat:@"%@",result[@"auth_code"]];
            NSString *member_id = [NSString stringWithFormat:@"%@",result[@"member_id"]];
            SetUserToken(auth_code);
            NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
            [userInfo setObject:member_id forKey:@"member_id"];
            [userInfo setObject:auth_code forKey:@"auth_code"];
            [[SYLoginDataMaster sharedInstance] saveLoginDataWithDic:userInfo];//保存登陆信息
            if (self.didLogin) {
                self.didLogin(message);
            }
            [self backClick:nil];
        }
    } error:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}



- (IBAction)backClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:NULL];
    
    
}

- (IBAction)registerClick:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"UserCenterStoryboard" bundle:nil];
    SYRegisterViewController *registerVC = [storyboard instantiateViewControllerWithIdentifier:@"SYRegisterViewController"];
    [self.navigationController pushViewController:registerVC animated:YES];
    [registerVC setDidRegister:^(NSString *userName, NSString *password) {
        self.userNameTextField.text = userName;
        self.passwordTextField.text = password;
        [self requestLogin];
    }];
}

- (IBAction)forgetPWClick:(id)sender {
}

- (IBAction)loginClick:(id)sender {
    if ([self.userNameTextField.text isEqualToString:@""]) {
        [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"请输入用户名"];
        return;
    }
    if ([self.passwordTextField.text isEqualToString:@""]) {
        [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"请输入密码"];
        return;
    }
    [self requestLogin];
}

- (IBAction)WZLLoginClik:(id)sender {
}
@end
